package com.nuanshui.heatedloan.msgcenter.service;

import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.msgcenter.dal.mapper.MessageMapper;
import com.nuanshui.heatedloan.msgcenter.facade.MessageService;
import com.nuanshui.heatedloan.msgcenter.facade.MsgReadStatusService;
import com.nuanshui.heatedloan.msgcenter.facade.MsgRedDotStatusService;
import com.nuanshui.heatedloan.msgcenter.form.MessageInfoForm;
import com.nuanshui.heatedloan.msgcenter.model.MessageInfo;
import com.nuanshui.heatedloan.msgcenter.model.MsgBizType;
import com.nuanshui.heatedloan.msgcenter.model.MsgOperationType;
import com.nuanshui.heatedloan.msgcenter.model.UserMessage;
import com.nuanshui.heatedloan.msgcenter.vo.MessageInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("messageService")
@Transactional(readOnly=true,value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
public class MessageServiceImpl implements MessageService {
	
	private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MsgRedDotStatusService msgRedDotStatusService;

    @Autowired
    private MsgReadStatusService msgReadStatusService;

    /**
     * 增加消息  
     * @param messageInfo 消息信息
     * @param userIds 影响到的用户
     */
    @Override
    @Transactional(readOnly=false,value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
    public void addMsgInfo(MessageInfoForm messageInfo, List<String> userIds) {
    	messageMapper.insert(messageInfo);
        for(String userId :userIds){
            UserMessage userMessage = new UserMessage();
            userMessage.setDstUserId(userId);
            userMessage.setMessageId(messageInfo.getId());
            messageMapper.insertUserMessage(userMessage);
        }
    }
    /**
     * 删除消息
     * @param messageInfo 消息内容
     */
    @Override
    @Transactional(readOnly=false,value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
    public void delMsgInfo(MessageInfo messageInfo) {
        messageMapper.updateDeleted(messageInfo);
    }
    /**
     * 获取用户的未读通知
     * @param userId 用户id
     * @param feedMessageTypeList 通知类型列表
     * @param pageable 页码
     * @return
     */

    @Override
    public List<MessageInfoVO> getMessageByUserIdAndType(String userId, List<String> feedMessageTypeList,Pageable pageable) {        List<String> userIds = new ArrayList<String>();
        userIds.add(userId);
        return messageMapper.selectByUserIdAndType(userIds,0,userId,feedMessageTypeList,pageable);
    }
    /**
     * 获取用户列表的的动态
     * @param userIds 用户列表
     * @param currentUserId 当前用户
     * @param feedMessageTypeList 动态的类型
     * @param pageable 页码
     * @
     */
    @Override
    public List<MessageInfoVO> getMessageByUserIdsAndType(List<String> userIds, String currentUserId, List<String> feedMessageTypeList, Pageable pageable) {
        return messageMapper.selectByUserIdAndType(userIds,null,currentUserId,feedMessageTypeList,pageable);
    }
	@Override
	public List<MessageInfoVO> findFriendMessage(List<String> userIds, String currentUserId, List<String> typelist,
			Pageable pageable) {
		
		return messageMapper.findFriendMessage(userIds, currentUserId, typelist, pageable);
	}

    @Override
    public List<MessageInfoVO> loadUserMaintainMsg(String userId, Pageable pager) {
        List<MessageInfoVO> messageInfoVOList = messageMapper.loadUserMaintainMsg(userId, pager);
        for(MessageInfoVO messageInfoVO : messageInfoVOList) {
            if(StringUtils.isEmpty(messageInfoVO.getMessageTypeName())) break;
            handlerMessageInfo(messageInfoVO);
        }
        return messageInfoVOList;
    }

    @Override
    public void handlerMessageInfo(MessageInfoVO messageInfoVO) {

    }

    @Override
    public void resetUserStatus(String userId, MsgBizType msgType) {
        if (SysStringUtils.isEmpty(userId))
            return;
        msgReadStatusService.resetReadNum(userId);
        msgRedDotStatusService.mark(userId, msgType, MsgOperationType.DELETE);
    }
}
